package com.wzh.springbootinit.exception;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wzh.springbootinit.common.BaseResponse;
import com.wzh.springbootinit.common.ErrorCode;
import com.wzh.springbootinit.util.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author wzh
 * @create 2023/4/1 20:31
 * @Description: 自定义全局异常处理器
 * 捕获所有异常，提供一致的处理方案，将异常转化为特定的格式返回给客户端
 */
//标记该类为全局异常处理器
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e){
//        记录异常信息
       log.error("businessExceptions: "+e.getMessage(),e);
       return ResultUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException: ",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage());
    }

}
